package cn.mengfly.testnrz.im.server.processer;

import cn.mengfly.testnrz.im.ProtoMsg;
import cn.mengfly.testnrz.im.server.ServerSession;
import cn.mengfly.testnrz.im.server.SessionMap;

import java.util.List;

/**
 * @author Mengfly
 */
public class ChatRedirectProcesser extends AbstractServerProcesser {
    @Override
    public ProtoMsg.HeadType type() {
        return ProtoMsg.HeadType.MESSAGE_REQUEST;
    }

    @Override
    public boolean action(ServerSession session, ProtoMsg.Message proto) {
        ProtoMsg.MessageRequest msg = proto.getMessageRequest();
        System.out.println("ChatMsg | from=" + msg.getFrom()
                + ", to" + msg.getTo() + ", content=" + msg.getContent());

        String to = msg.getTo();
        List<ServerSession> sessionBy = SessionMap.inst().getSessionBy(to);
        if (sessionBy == null || sessionBy.isEmpty()) {
            System.out.println("[" + to + "] 不在线，发送失败");
        } else {
            sessionBy.forEach(serverSession -> serverSession.writeAndFlush(proto));
        }
        return true;
    }
}
